package com.vim.modules.web.utils;

import com.vim.modules.web.model.Table;

import java.util.HashMap;

/**
 * 代码自动生成工具-生成对应的JAVA
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CreateJava {

    public final String JAVA_PATH = "src/main/java/";
    public final String RESOURCES_PATH = "src/main/resources/";

	private String javaPath;
	private String resourcePath;
	private String packagePath;

	public CreateJava(){}
    private static CreateJava _instance = new CreateJava();
    public static CreateJava getInstance() { return _instance; }

    /**
     * 创建存放目录
     */
    public void createDictory(String rootPath, String packagePath) {
    	this.javaPath = rootPath+ JAVA_PATH+packagePath.replaceAll("\\.", "/") + "/";
    	this.resourcePath = rootPath+ RESOURCES_PATH;
    	this.packagePath = packagePath;

        FileUtil.createDictory(rootPath + JAVA_PATH);
        FileUtil.createDictory(rootPath + RESOURCES_PATH);

        FileUtil.createDictory(this.javaPath);
        FileUtil.createDictory(this.javaPath + "dao/");
        FileUtil.createDictory(this.javaPath + "model/");
        FileUtil.createDictory(this.javaPath + "service/");
        FileUtil.createDictory(this.javaPath + "api/");
        FileUtil.createDictory(this.javaPath + "controller/");
        FileUtil.createDictory(this.resourcePath+"mapper/");
        FileUtil.createDictory(this.resourcePath+"public/module/plugins");
        FileUtil.createDictory(this.resourcePath+"public/module/views");
    }
    
    /**
     * 创建文件
     */
    public void createFile(Table table){
    	this.createModel(table);
    	this.createDAO(table);
    	this.createService(table);
    	this.createApi(table);
    	this.createController(table);
    	this.createMapper(table);
    	this.createListView(table);
    	this.createEditView(table);
    }

	private void createModel(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", this.packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "model.ftl");
        FileUtil.writeFile(s, this.javaPath + "model/" + table.getClassName() + ".java", false);
	}
	
	private void createDAO(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "dao.ftl");
        FileUtil.writeFile(s, this.javaPath + "dao/" + table.getClassName() + "Mapper.java", false);
    }
	
	private void createService(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "service.ftl");
        FileUtil.writeFile(s, this.javaPath + "service/" + table.getClassName() + "Service.java", false);
    }

    private void createApi(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "api.ftl");
        FileUtil.writeFile(s, this.javaPath + "api/TestController.java", false);
    }

	private void createController(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "controller.ftl");
        FileUtil.writeFile(s, this.javaPath + "controller/" + table.getClassName() + "Controller.java", false);
    }
	
	private void createMapper(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "mapper.ftl");
        FileUtil.writeFile(s, this.resourcePath+"mapper/" + table.getClassName() + ".xml", false);
    }

    private void createListView(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "list.ftl");
        FileUtil.writeFile(s, this.resourcePath+"public/module/views/"+"list.html", false);
    }

    private void createEditView(Table table) {
        HashMap map = new HashMap();
        map.put("tableName", table.getTableName());
        map.put("className", table.getClassName());
        map.put("beanName", table.getBeanName());
        map.put("packagePath", packagePath);
        map.put("table", table);
        String s = FreemarkerManager.getInstance().process(map, "edit.ftl");
        FileUtil.writeFile(s, this.resourcePath+"public/module/views/"+"edit.html", false);
    }

}	